package n11;

/**
 * @author 苏文广
 * @since 2021/10/8
 */
public class TwoPointSolution {

    // 双指针, 指针从中间开始, 往两边移动
    public static void main(String[] args) {
        ViolentSolution.Solution solution = new ViolentSolution.Solution();
        int i = solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
        System.out.println(i);
    }

    static class Solution {
        public int maxArea(int[] height) {
            if (height == null || height.length < 2) {
                return 0;
            }

            int max = 0;

            // 双指针
            // left
            int left = 0;
            // right
            int right = height.length - 1;

            // 解决谁移动的问题即可
            // 向内移动, x 肯定越来越小, 选择高不动. 矮的先动
            while (left < right) {
                int temp = (right - left) * Math.min(height[left], height[right]);
                max = Math.max(max, temp);

                if (height[left] > height[right]) {
                    right--;
                } else {
                    left++;
                }
            }

            return max;
        }
    }
}
